// ========================================================================
// Copyright (c) 2022 ChangYou, All rights reserved.
// http://www.changyou.com/
// 
// Filename:    MeshEditor.cs
// Time:        2022-08-24 20:49:14
// Author:      xiangjinbao
// Email:       xiangjinbao@cyou-inc.com
// Version:     v2022.0.1
// Description: MeshEditor
// ========================================================================

using UnityEngine;

namespace Editor
{
    public class MeshEditorUtility : UnityEditor.Editor
    {
        /// <summary>
        /// 保存Mesh到本地
        /// </summary>
        /// <param name="mesh"></param>
        /// <param name="name"></param>
        /// <param name="makeNewInstance"></param>
        /// <param name="optimizeMesh"></param>
        public static void SaveMesh(Mesh mesh, string name, bool makeNewInstance, bool optimizeMesh)
        {
            string path = UnityEditor.EditorUtility.SaveFilePanel("Save Mesh Asset", "Assets/", name, "mesh");
            if (string.IsNullOrEmpty(path))
            {
                return;
            }
 
            path = UnityEditor.FileUtil.GetProjectRelativePath(path);
 
            Mesh meshToSave = (makeNewInstance) ? Object.Instantiate(mesh) as Mesh : mesh;

            if (optimizeMesh)
            {
                UnityEditor.MeshUtility.Optimize(meshToSave);
            }
 
            UnityEditor.AssetDatabase.CreateAsset(meshToSave, path);
            UnityEditor.AssetDatabase.SaveAssets();
        }
    }
}
